# -*- coding: UTF-8 -*-
import scrapy
import messAround.util.help as util


# Acfun热门排行榜
# https://www.acfun.cn/rank/list
# scrapy crawl acfun_search
class AcfunTopSpider(scrapy.Spider):
    source = 0

    name = 'acfun_search'

    allowed_domains = ['www.acfun.cn']

    start_urls = ['https://www.acfun.cn/search']

    headers = {
        'User-Agent': util.agent,
        ':authority': 'www.acfun.cn',
        'referer': 'https://www.acfun.cn/rank/list',
        'sec-fetch-dest': 'empty',
        'Sec-Fetch-mode': 'cors',
        'Sec-Fetch-site': 'same-origin',
        'x-requested-with': 'XMLHttpRequest',
    }

    custom_settings = {
        'ITEM_PIPELINES': {
            'messAround.pipeline.acfun.AcfunSearchPipeline': 300
        }
    }

    def parse(self, response):
        for index in range(1, 8):
            item_xpath = '/html/body/div[3]/div[2]/div/div/div/div[1]/ul/li[' + str(index) + ']/a/b'
            keyword = response.xpath(item_xpath + '/text()').get()
            link = 'https://www.acfun.cn/search?keyword=' + keyword
            yield {
                'no': index,
                'keyword': keyword,
                'link': link,
            }
        pass
